﻿#pragma once
#include "APIUtils.h"
namespace rjksClient {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	/// <summary>
	/// FlatForm 摘要
	/// </summary>
	public ref class FlatForm : public System::Windows::Forms::Form
	{
	public:
		FlatForm(void)
		{
			InitializeComponent();
			//
			//TODO: 在此处添加构造函数代码
			//
		}
	private: System::Windows::Forms::Button^  btnFlatUIClose;
			System::Windows::Forms::Button^  btnFlatUIMax;
			System::Windows::Forms::Button^  btnFlatUIMin;
	public : static int MAXMIN=0x2;
			static int MIN_ONLY=0x1;
			static int MAXMIN_NONE=0x0;
	private: System::Windows::Forms::Label^  lblFlatUITitle;
			 static int cGrip = 10;      // Grip size
			static int cCaption = 25;   // Caption bar height;
	private: System::Windows::Forms::Label^  boarderLeft;
	private: System::Windows::Forms::Label^  boarderRight;
	private: System::Windows::Forms::Label^  boarderBottom;

	public: 
		int MaxMinConfig;

			 
        /// <summary>
        /// Handling the window messages
        /// </summary>
        /// <param name="m"></param>
	protected: virtual void WndProc(System::Windows::Forms::Message %m) override
			{
				__super::WndProc(m);
				//if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)
				//	m.Result = (IntPtr)HTCAPTION;//total form as caption
				if (m.Msg == WM_NCHITTEST) { 
					Point pos = Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
					pos = this->PointToClient(pos);
					/*if (pos.Y < cCaption) {
						m.Result = (IntPtr)HTCAPTION;  // HTCAPTION
						return;
					}*/
					if(this->WindowState==FormWindowState::Minimized)
						return;
					bool xr_out=pos.X >= this->ClientSize.Width - cGrip;
					bool y_out=pos.Y >= this->ClientSize.Height - cGrip;
					bool xl_out=pos.X <= cGrip;
					if (xr_out)//right boarder
					{
						if (y_out ) //bottom boarder
							m.Result = (IntPtr)HTBOTTOMRIGHT; 
						else
							m.Result = (IntPtr)HTRIGHT; 
					}else if (y_out)//
					{
						if (xl_out)
							m.Result = (IntPtr)HTBOTTOMLEFT; 
						else
							m.Result = (IntPtr)HTBOTTOM; 
					}else if (xl_out)
					{
						m.Result = (IntPtr)HTLEFT; 
					}
				}
			}
			   virtual void OnPaint(PaintEventArgs^ e) override
			{
			
			}
	protected:
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		~FlatForm()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->btnFlatUIClose = (gcnew System::Windows::Forms::Button());
			this->btnFlatUIMax = (gcnew System::Windows::Forms::Button());
			this->btnFlatUIMin = (gcnew System::Windows::Forms::Button());
			this->lblFlatUITitle = (gcnew System::Windows::Forms::Label());
			this->boarderLeft = (gcnew System::Windows::Forms::Label());
			this->boarderRight = (gcnew System::Windows::Forms::Label());
			this->boarderBottom = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// btnFlatUIClose
			// 
			this->btnFlatUIClose->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(149)), static_cast<System::Int32>(static_cast<System::Byte>(165)), 
				static_cast<System::Int32>(static_cast<System::Byte>(166)));
			this->btnFlatUIClose->FlatAppearance->BorderSize = 0;
			this->btnFlatUIClose->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(216)), 
				static_cast<System::Int32>(static_cast<System::Byte>(53)), static_cast<System::Int32>(static_cast<System::Byte>(0)));
			this->btnFlatUIClose->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnFlatUIClose->Font = (gcnew System::Drawing::Font(L"Consolas", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->btnFlatUIClose->ForeColor = System::Drawing::Color::White;
			this->btnFlatUIClose->Location = System::Drawing::Point(4, 0);
			this->btnFlatUIClose->Name = L"btnFlatUIClose";
			this->btnFlatUIClose->Size = System::Drawing::Size(53, 23);
			this->btnFlatUIClose->TabIndex = 0;
			this->btnFlatUIClose->Tag = L"关闭当前窗口";
			this->btnFlatUIClose->Text = L"Χ";
			this->btnFlatUIClose->TextAlign = System::Drawing::ContentAlignment::BottomCenter;
			this->btnFlatUIClose->UseVisualStyleBackColor = false;
			this->btnFlatUIClose->Click += gcnew System::EventHandler(this, &FlatForm::btnFlatUIClose_Click);
			// 
			// btnFlatUIMax
			// 
			this->btnFlatUIMax->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->btnFlatUIMax->FlatAppearance->BorderSize = 0;
			this->btnFlatUIMax->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(2)), 
				static_cast<System::Int32>(static_cast<System::Byte>(131)), static_cast<System::Int32>(static_cast<System::Byte>(236)));
			this->btnFlatUIMax->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnFlatUIMax->Font = (gcnew System::Drawing::Font(L"宋体", 10.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->btnFlatUIMax->ForeColor = System::Drawing::Color::White;
			this->btnFlatUIMax->Location = System::Drawing::Point(59, 0);
			this->btnFlatUIMax->Name = L"btnFlatUIMax";
			this->btnFlatUIMax->Size = System::Drawing::Size(27, 23);
			this->btnFlatUIMax->TabIndex = 1;
			this->btnFlatUIMax->Text = L"▭";
			this->btnFlatUIMax->TextAlign = System::Drawing::ContentAlignment::BottomCenter;
			this->btnFlatUIMax->UseVisualStyleBackColor = false;
			this->btnFlatUIMax->Click += gcnew System::EventHandler(this, &FlatForm::btnFlatUIMax_Click);
			// 
			// btnFlatUIMin
			// 
			this->btnFlatUIMin->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->btnFlatUIMin->FlatAppearance->BorderSize = 0;
			this->btnFlatUIMin->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(2)), 
				static_cast<System::Int32>(static_cast<System::Byte>(131)), static_cast<System::Int32>(static_cast<System::Byte>(236)));
			this->btnFlatUIMin->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnFlatUIMin->Font = (gcnew System::Drawing::Font(L"Times New Roman", 13.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->btnFlatUIMin->ForeColor = System::Drawing::Color::White;
			this->btnFlatUIMin->Location = System::Drawing::Point(88, 0);
			this->btnFlatUIMin->Name = L"btnFlatUIMin";
			this->btnFlatUIMin->Size = System::Drawing::Size(27, 23);
			this->btnFlatUIMin->TabIndex = 2;
			this->btnFlatUIMin->Text = L"━";
			this->btnFlatUIMin->TextAlign = System::Drawing::ContentAlignment::BottomCenter;
			this->btnFlatUIMin->UseVisualStyleBackColor = false;
			this->btnFlatUIMin->Click += gcnew System::EventHandler(this, &FlatForm::btnFlatUIMin_Click);
			// 
			// lblFlatUITitle
			// 
			this->lblFlatUITitle->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->lblFlatUITitle->Cursor = System::Windows::Forms::Cursors::Default;
			this->lblFlatUITitle->Dock = System::Windows::Forms::DockStyle::Top;
			this->lblFlatUITitle->Font = (gcnew System::Drawing::Font(L"微软雅黑", 10.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->lblFlatUITitle->ForeColor = System::Drawing::Color::White;
			this->lblFlatUITitle->Location = System::Drawing::Point(0, 0);
			this->lblFlatUITitle->Name = L"lblFlatUITitle";
			this->lblFlatUITitle->Size = System::Drawing::Size(504, 26);
			this->lblFlatUITitle->TabIndex = 4;
			this->lblFlatUITitle->Text = L"title";
			this->lblFlatUITitle->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
			this->lblFlatUITitle->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &FlatForm::lblFlatUITitle_MouseDown);
			// 
			// boarderLeft
			// 
			this->boarderLeft->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->boarderLeft->Dock = System::Windows::Forms::DockStyle::Left;
			this->boarderLeft->Location = System::Drawing::Point(0, 26);
			this->boarderLeft->Name = L"boarderLeft";
			this->boarderLeft->Size = System::Drawing::Size(5, 322);
			this->boarderLeft->TabIndex = 5;
			// 
			// boarderRight
			// 
			this->boarderRight->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->boarderRight->Dock = System::Windows::Forms::DockStyle::Right;
			this->boarderRight->Location = System::Drawing::Point(499, 26);
			this->boarderRight->Name = L"boarderRight";
			this->boarderRight->Size = System::Drawing::Size(5, 322);
			this->boarderRight->TabIndex = 6;
			// 
			// boarderBottom
			// 
			this->boarderBottom->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(126)), 
				static_cast<System::Int32>(static_cast<System::Byte>(34)));
			this->boarderBottom->Dock = System::Windows::Forms::DockStyle::Bottom;
			this->boarderBottom->Location = System::Drawing::Point(5, 343);
			this->boarderBottom->Name = L"boarderBottom";
			this->boarderBottom->Size = System::Drawing::Size(494, 5);
			this->boarderBottom->TabIndex = 7;
			// 
			// FlatForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(504, 348);
			this->Controls->Add(this->boarderBottom);
			this->Controls->Add(this->boarderRight);
			this->Controls->Add(this->boarderLeft);
			this->Controls->Add(this->btnFlatUIMin);
			this->Controls->Add(this->btnFlatUIMax);
			this->Controls->Add(this->btnFlatUIClose);
			this->Controls->Add(this->lblFlatUITitle);
			this->DoubleBuffered = true;
			this->Font = (gcnew System::Drawing::Font(L"微软雅黑", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
			this->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
			this->Name = L"FlatForm";
			this->Text = L"FlatForm";
			this->Load += gcnew System::EventHandler(this, &FlatForm::FlatForm_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void btnFlatUIMin_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->WindowState=FormWindowState::Minimized;
			 }
	public:  System::Void btnFlatUIMax_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (this->Dock==DockStyle::Fill)
				 {
					 this->Dock=DockStyle::None;
					 this->MdiParent->Text="课程设计交流系统";
				 }
				 else
				 {
					 this->Dock=DockStyle::Fill;
					 this->MdiParent->Text+=" ["+this->lblFlatUITitle->Text+"]";
				 }
					//this->WindowState=FormWindowState::Maximized;
			 }
	public:	System::Void btnFlatUIClose_Click(System::Object^  sender, System::EventArgs^  e) {
				
				if(this->MdiParent!=nullptr && this->MdiParent->MdiChildren->Length==1)//login or last form
					Application::Exit();
				this->Close();
			}
	private:		void visiblizeControls(){
				if (this->MaxMinConfig<MAXMIN)
				{
					this->btnFlatUIMax->Visible=false;
					this->btnFlatUIMin->Location=this->btnFlatUIMax->Location;
				}
				if (this->MaxMinConfig==MAXMIN_NONE)
					this->btnFlatUIMin->Visible=false;
			}
			System::Void FlatForm_Load(System::Object^  sender, System::EventArgs^  e) {
				visiblizeControls();
				setTitleText(this->Text);
			 }
public :void setBoarderColor(Color color)
		{
			this->lblFlatUITitle->BackColor=color;
			this->btnFlatUIMax->BackColor=color;
			this->btnFlatUIMin->BackColor=color;
			this->boarderBottom->BackColor=color;
			this->boarderLeft->BackColor=color;
			this->boarderRight->BackColor=color;
		}
		void setBoarderColor(int argb)
		{
			if (argb<=0xFFFFFF)//no alpha
				argb+=0xDD000000;
			setBoarderColor(Color::FromArgb(argb));
		}

		void setTitleText(String^ txt)
		{
			this->Text=txt;
			this->lblFlatUITitle->Text=txt;
		}
		 static double UTime(DateTime time)
        {
            double intResult = 0;
            DateTime startTime = TimeZone::CurrentTimeZone->ToLocalTime(DateTime(1970, 1, 1));
            intResult = (time - startTime).TotalSeconds;
            return intResult;
        }
private: static double lastclick=UTime(DateTime::Now);
		  System::Void lblFlatUITitle_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			 if (e->Button == ::MouseButtons::Left)
			{
				if ( UTime(DateTime::Now)-lastclick<0.3)
				{
					if (this->WindowState==FormWindowState::Maximized)
					{
						this->WindowState=FormWindowState::Normal;
					}else if (this->Dock==DockStyle::Fill)
						this->Dock=DockStyle::None;
					else
					{
						this->Dock=DockStyle::Fill;
						//use dockstyle instead of maxiumum to avoid showing ctrl box on parent
					}
					lastclick=0;
				}else
				{
					APIUtils::ReleaseCapture();
					APIUtils::EnableDrag(Handle);
					lastclick=UTime(DateTime::Now);
				}
			}
		 }
private: System::Void boarderLeft_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			 APIUtils::SendMessage(this->Handle,WM_NCLBUTTONDOWN,HTLEFT,0);
		 }
private: System::Void boarderBottom_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			 APIUtils::SendMessage(this->Handle,WM_NCLBUTTONDOWN,HTBOTTOM,0);
		 }
private: System::Void boarderRight_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
			  APIUtils::SendMessage(this->Handle,WM_NCLBUTTONDOWN,HTRIGHT,0);
		 }
};
};
